package com.orangelife.service.PropFeeType.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.PropFeeType.IPropFeeTypeService;
import com.orangelife.service.PropFeeType.model.PropFeeTypeDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.PropertyUtils;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_13006015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeType类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-03-07 16:43:12
 * ==============================================================
 */
@Service("propFeeTypeService")
public class PropFeeTypeServiceImpl extends MybaitsGenericDaoSubLogic implements
		IPropFeeTypeService {
	private static Logger logger = Logger
			.getLogger(PropFeeTypeServiceImpl.class);

	@Override
	public List<Map<String, Object>> getPropFeeType(String commID)
			throws AppBizException {
		Map<String, Object> param = new HashMap<String, Object>();

		param.put("commID", commID);
		// TODO Auto-generated method stub
		return this.getSqlSession().selectList(
				"PropFeeTypeMapper.getFeeTypeByCommID", param);
	}

	@Override
	public List<Map<String, Object>> getPropFeeTypeCode(String[] commIDs)
			throws AppBizException {

		Map<String, Object> param = new HashMap<String, Object>();

		param.put("commIDs", commIDs);
		// TODO Auto-generated method stub
		return this.getSqlSession().selectList(
				"PropFeeTypeMapper.getPropFeeTypeCode", param);

	}

	@Override
	public List<Map<String, Object>> getPropFeeTypeCode(String[] commIDs,
			String[] codes) throws AppBizException {

		Map<String, Object> param = new HashMap<String, Object>();

		param.put("commIDs", commIDs);

		if (codes != null && codes.length > 0) {

			param.put("codes", codes);
		}

		// TODO Auto-generated method stub
		return this.getSqlSession().selectList(
				"PropFeeTypeMapper.getPropFeeTypeCode", param);

	}

	@Override
	public List<PropFeeTypeDto> selectByPrimaryKey(String feeTypeId)
			throws AppBizException {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("feeTypeId", feeTypeId);
		return this.getSqlSession().selectList(
				"PropFeeTypeDtoMapper.selectByPrimaryKey", param);
	}

	@Override
	public List<PropFeeTypeDto> selectByCommID(String commID)
			throws AppBizException {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("commID", commID);
		return this.getSqlSession().selectList(
				"PropFeeTypeDtoMapper.selectByCommID", param);
	}

	@Override
	public void insertSelective(PropFeeTypeDto propFeeTypeDto)
			throws AppBizException {
		if (StringUtils.isBlank(propFeeTypeDto.getItemStat())) {

			propFeeTypeDto.setItemStat("1");
		}
		this.getSqlSession().insert("PropFeeTypeDtoMapper.insertSelective",
				propFeeTypeDto);

	}

	@Override
	public void updateByPrimaryKeySelective(PropFeeTypeDto propFeeTypeDto)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeTypeDto.getItemStat())) {

			propFeeTypeDto.setItemStat("1");
		}
		this.getSqlSession().update(
				"PropFeeTypeDtoMapper.updateByPrimaryKeySelective",
				propFeeTypeDto);

	}

	@Override
	public void deleteByPrimaryKey(String feeTypeId) throws AppBizException {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("feeTypeId", feeTypeId);
		this.getSqlSession().delete("PropFeeTypeDtoMapper.deleteByPrimaryKey",
				param);

	}

	@Override
	public void updateItemStat(String feeTypeId) throws AppBizException {

		PropFeeTypeDto propFeeTypeDto = new PropFeeTypeDto();
		propFeeTypeDto.setItemStat("0");
		propFeeTypeDto.setFeeTypeID(feeTypeId);

		this.getSqlSession().update(
				"PropFeeTypeDtoMapper.updateByPrimaryKeySelective",
				propFeeTypeDto);

	}

	@Override
	public List<PropFeeTypeDto> selectByPropFeeTypeDto(
			PropFeeTypeDto propFeeTypeDto) throws AppBizException {
		return this.getSqlSession().selectList(
				"PropFeeTypeDtoMapper.selectByPropFeeTypeDto", propFeeTypeDto);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeType(PropFeeTypeDto propFeeType)
			throws AppBizException {

		Assert.notNull(propFeeType);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		Assert.notNull(propFeeType);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeType.getFeeTypeName())) {
			if (propFeeType.getFeeTypeName().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "类目名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeType.getRemark())) {
			if (propFeeType.getRemark().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "备注长度超出");
			}
		}

		if (StringUtils.isBlank(propFeeType.getType())) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					"0:固定 1:非固定不能为空");
		}

		if (!StringUtils.isBlank(propFeeType.getType())) {
			if (propFeeType.getType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"0:固定 1:非固定长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeType.getCode())) {
			if (propFeeType.getCode().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeType.getGroupID())) {
			if (propFeeType.getGroupID().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("feeTypeName", propFeeType.getFeeTypeName());
		map.put("commID", propFeeType.getCommID());
		map.put("code", propFeeType.getCode());
		// map.put("feeTypePID", propFeeType.getFeeTypePID());
		List<Map<String, Object>> propFeeTypeDtos = this
				.bauthfindPropFeeTypeByMap(map);
		if (propFeeTypeDtos != null && propFeeTypeDtos.size() > 0) {

			throw new AppBizException(2, "亲，该信息已经存在");
		}

		propFeeType.setFeeTypeID(IdUtil.getuuId());
		propFeeType.setItemStat(TypeConst.FLAG_ON);
		propFeeType.setTscreate(new Date());
		propFeeType.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeTypeDtoMapper.insertSelective", propFeeType);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeType(PropFeeTypeDto propFeeType)
			throws AppBizException {
		Assert.notNull(propFeeType);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeType.getFeeTypeID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeTypeDto oldPropFeeType = this.findPropFeeTypeByID(propFeeType
				.getFeeTypeID());

		if (!StringUtils.isBlank(propFeeType.getFeeTypeName())) {
			if (propFeeType.getFeeTypeName().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "类目名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeType.getRemark())) {
			if (propFeeType.getRemark().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "备注长度超出");
			}
		}

		if (StringUtils.isBlank(propFeeType.getType())) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					"0:固定 1:非固定不能为空");
		}

		if (!StringUtils.isBlank(propFeeType.getType())) {
			if (propFeeType.getType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"0:固定 1:非固定长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeType.getCode())) {
			if (propFeeType.getCode().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeType.getGroupID())) {
			if (propFeeType.getGroupID().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}

		if (oldPropFeeType == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("feeTypeName", propFeeType.getFeeTypeName());
		map.put("commID", propFeeType.getCommID());
		// map.put("code", propFeeType.getCode());

		map.put("level", "3");
		// map.put("feeTypePID", propFeeType.getFeeTypePID());
		List<Map<String, Object>> propFeeTypeDtos = PropertyUtils
				.turnToEntity(this.bauthfindPropFeeTypeByMap(map));
		if (propFeeTypeDtos != null && propFeeTypeDtos.size() > 1) {
			throw new AppBizException(2, "亲，费用名称已经存在");
		}
		if (propFeeTypeDtos != null && propFeeTypeDtos.size() == 1) {
			if (!propFeeTypeDtos.get(0).get("feeTypeID").toString()
					.equals(propFeeType.getFeeTypeID())) {
				throw new AppBizException(2, "亲，费用名称已经存在");
			}
		}

		/***
		 * 非其它费用默认收取到物业费
		 */
		if (!StringUtils.isBlank(propFeeType.getType())) {
			if (!propFeeType.getType().equals("3")) {
				propFeeType.setBelongType("1");
			}
		}
		propFeeType.setTsedit(new Date());
		return this.getSqlSession()
				.update("PropFeeTypeDtoMapper.updateByPrimaryKeySelective",
						propFeeType);
	}

	@Override
	public PropFeeTypeDto findPropFeeTypeByID(String propFeeTypeID)
			throws AppBizException {
		if (StringUtils.isBlank(propFeeTypeID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeTypeDto oldPropFeeType = this.getSqlSession().selectOne(
				"PropFeeTypeSearchMapper.selectByPrimaryKey", propFeeTypeID);
		if (oldPropFeeType == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeType.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeType;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeTypeByLogic(String propFeeTypeID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeTypeID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("feeTypeID", propFeeTypeID);

		List<Map<String, Object>> propFeeDetails = this
				.getSqlSession()
				.selectList(
						"PropFeeTypeSearchMapper.selectPropFeeDetailDtoByRelevantID",
						map);

		if (propFeeDetails != null && propFeeDetails.size() > 0) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "亲，该类型已经被使用无法删除");
		}

		PropFeeTypeDto oldPropFeeType = this.findPropFeeTypeByID(propFeeTypeID);

		oldPropFeeType.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeType.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeTypeDtoMapper.updateByPrimaryKeySelective",
				oldPropFeeType);
	}

	@Override
	public List<Map<String, Object>> findPropFeeTypeByPage(String type,
			String commID, String tsCreate, String search, int pageNo,
			int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		map.put("commID", commID);
		map.put("type", type);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeTypeSearchMapper.findAllPropFeeType", map);

		/**
		 * if (list != null && list.size() > 0) { List<String> commIDs = new
		 * ArrayList<String>(); for (Map<String, Object> propFeeMeter : list) {
		 * commIDs.add(propFeeMeter.get("comm_id").toString()); }
		 * List<Map<String, Object>> comms = com.orangelife.utils.JsonMapUtils
		 * .getPageEntityMap(HttpSvcUtils.doPostSvc(UrlPath.bauthComm, commIDs,
		 * null));
		 * 
		 * Map<String, String> connValue = new HashMap<String, String>();
		 * 
		 * connValue.put("comm", "comm"); list = SplicingUtils.connoList(list,
		 * comms, "comm_id", "commID", connValue);
		 * 
		 * }
		 **/

		return list;
	}

	@Override
	public List<PropFeeTypeDto> getPropFeeTypeByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeTypeDto> list = this.getSqlSession().selectList(
				"PropFeeTypeSearchMapper.getAllPropFeeTypeDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeTypeByRelevantID(
			String feeTypeID, String commID, String feeTypeName, String type,
			String belongType, String feeTypePID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("feeTypeID", feeTypeID);
		map.put("commID", commID);
		map.put("feeTypeName", feeTypeName);
		map.put("type", type);
		map.put("belongType", belongType);
		map.put("feeTypePID", feeTypePID);

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeTypeSearchMapper.selectPropFeeTypeDtoyRelevantID", map);
		return list;
	}

	@Override
	public List<PropFeeTypeDto> findPropFeeTypeByRelevantID(String feeTypeID,
			String commID, String feeTypeName, String type, String belongType,
			String feeTypePID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("feeTypeID", feeTypeID);
		map.put("commID", commID);
		map.put("feeTypeName", feeTypeName);
		map.put("type", type);
		map.put("belongType", belongType);
		map.put("feeTypePID", feeTypePID);

		List<PropFeeTypeDto> list = this.getSqlSession().selectList(
				"PropFeeTypeSearchMapper.findPropFeeTypeDtoByRelevantID", map);
		return list;
	}

	@Override
	public List<PropFeeTypeDto> bauthfindPropFeeType(List<String> propFeeTypeIDs)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeTypeDto> list = this.getSqlSession().selectList(
				"PropFeeTypeSearchMapper.batchFindPropFeeType", propFeeTypeIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeTypeByMap(
			Map<String, Object> propFeeType) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeTypeSearchMapper.batchFindPropFeeTypeByMap",
				propFeeType);

		if (propFeeType.containsKey("isTop")) {
			if (propFeeType.get("isTop") != null) {
				if (propFeeType.get("isTop").toString().equals("1")) {
					if (list != null && list.size() > 0) {
						propFeeType.remove("isTop");
						propFeeType.remove("type");
						// propFeeType.put("feeTypePID", "0");
						List<String> feeTypeIDs = new ArrayList<String>();
						for (Map<String, Object> map : list) {
							feeTypeIDs.add(map.get("fee_type_pid").toString());
						}
						propFeeType.put("feeTypeIDs", feeTypeIDs);

						List<Map<String, Object>> parTypes = this
								.getSqlSession()
								.selectList(
										"PropFeeTypeSearchMapper.batchFindPropFeeTypeByMap",
										propFeeType);
						list.addAll(parTypes);

					}
				}

			}
		}

		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		// TODO Auto-generated method stub

	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeTypes(PropFeeTypeDto propFeeType)
			throws AppBizException {
		Assert.notNull(propFeeType);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		Assert.notNull(propFeeType);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeType.getFeeTypeName())) {
			if (propFeeType.getFeeTypeName().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "类目名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeType.getRemark())) {
			if (propFeeType.getRemark().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "备注长度超出");
			}
		}

		if (StringUtils.isBlank(propFeeType.getType())) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					"0:固定 1:非固定不能为空");
		}

		if (!StringUtils.isBlank(propFeeType.getType())) {
			if (propFeeType.getType().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"0:固定 1:非固定长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeType.getCode())) {
			if (propFeeType.getCode().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeType.getGroupID())) {
			if (propFeeType.getGroupID().length() > 36) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isBlank(propFeeType.getFeeTypePID())) {
			propFeeType.setFeeTypePID("0");
			propFeeType.setLevel(1);
		} else {
			PropFeeTypeDto oldPropFeeType = this
					.findPropFeeTypeByID(propFeeType.getFeeTypePID());
			propFeeType.setLevel(oldPropFeeType.getLevel() + 1);
		}
		map.put("feeTypeName", propFeeType.getFeeTypeName());
		map.put("commID", propFeeType.getCommID());
		// map.put("code", propFeeType.getCode());
		// map.put("feeTypePID", propFeeType.getFeeTypePID());

		map.put("level", "3");
		List<Map<String, Object>> propFeeTypeDtos = this
				.bauthfindPropFeeTypeByMap(map);
		if (propFeeTypeDtos != null && propFeeTypeDtos.size() > 0) {

			throw new AppBizException(2, "亲，该信息已经存在");
		}

		propFeeType.setFeeTypeID(IdUtil.getuuId());
		propFeeType.setItemStat(TypeConst.FLAG_ON);
		propFeeType.setTscreate(new Date());
		propFeeType.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeTypeDtoMapper.insertSelective", propFeeType);
	}

	@Override
	public List<Map<String, Object>> findPropFeeTypeInfos(String type,
			String commID, String tsCreate, String search, int pageNo,
			int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		map.put("type", type);
		map.put("commID", commID);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeTypeSearchMapper.findPropFeeTypeInfos", map);
		/**
		 * if (list != null && list.size() > 0) { List<String> commIDs = new
		 * ArrayList<String>(); for (Map<String, Object> propFeeMeter : list) {
		 * commIDs.add(propFeeMeter.get("comm_id").toString()); }
		 * List<Map<String, Object>> comms = com.orangelife.utils.JsonMapUtils
		 * .getPageEntityMap(HttpSvcUtils.doPostSvc(UrlPath.bauthComm, commIDs,
		 * null));
		 * 
		 * Map<String, String> connValue = new HashMap<String, String>();
		 * 
		 * connValue.put("comm", "comm"); list = SplicingUtils.connoList(list,
		 * comms, "comm_id", "commID", connValue);
		 * 
		 * }
		 */

		return list;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeType(List<PropFeeTypeDto> propFeeType)
			throws AppBizException {
		if (propFeeType != null && propFeeType.size() > 0) {
			return this.getSqlSession().insert(
					"PropFeeTypeBauthMapper.bauthInsert", propFeeType);
		}

		return 0;
	}

	@Override
	public List<Map<String, Object>> getPropFeeTypeLevel(String commID,
			String[] codes) throws AppBizException {
		Map<String, Object> param = new HashMap<String, Object>();

		param.put("commID", commID);

		if (codes != null && codes.length > 0) {

			param.put("codes", codes);
		}

		// TODO Auto-generated method stub
		return this.getSqlSession().selectList(
				"PropFeeTypeMapper.getPropFeeTypeLevel", param);
	}

	@Override
	public Map<String, Map<String, Object>> getPropFeeTypeLevelForMap(
			String commID, String[] codes) throws AppBizException {
		Map<String, Object> param = new HashMap<String, Object>();

		param.put("commID", commID);

		if (codes != null && codes.length > 0) {

			param.put("codes", codes);
		}

		// TODO Auto-generated method stub
		return this.getSqlSession().selectMap(
				"PropFeeTypeMapper.getPropFeeTypeLevel", param, "feeTypeID");
	}

	@Override
	public List<Map<String, Object>> getFeeTypeTree(String commID)
			throws AppBizException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		return this.getSqlSession().selectList(
				"PropFeeTypeSearchMapper.getFeeTypeTree", map);
	}

	@Override
	public List<Map<String, Object>> getPropFeeTypeCodeByLevel(
			String[] commIDs, String[] codes, String level)
			throws AppBizException {
		Map<String, Object> param = new HashMap<String, Object>();

		param.put("commIDs", commIDs);
		param.put("level", level);
		if (codes != null && codes.length > 0) {

			param.put("codes", codes);
		}

		// TODO Auto-generated method stub
		return this.getSqlSession().selectList(
				"PropFeeTypeMapper.getPropFeeTypeCodeByLevel", param);
	}

	@Override
	public List<Map<String, Object>> getFnumber(String commID)
			throws AppBizException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		return this.getSqlSession().selectList(
				"PropFeeTypeSearchMapper.getFnumber", map);
	}

	@Override
	public List<Map<String, Object>> getFeeTypeByFeeDetail(
			Map<String, Object> map) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeTypeSearchMapper.getFeeTypeByFeeDetail", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeTypeByMap(
			Map<String, Object> params) throws AppBizException {

		return this.getSqlSession().selectList(
				"PropFeeTypeMapper.selectPropFeeTypeByMap", params);
	}

	@Override
	public List<PropFeeTypeDto> findPropFeeTypeByRelevantID(String feeTypeID,
			String commID, String feeTypeName, String type, String belongType,
			String feeTypePID, String code) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("feeTypeID", feeTypeID);
		map.put("commID", commID);
		map.put("feeTypeName", feeTypeName);
		map.put("type", type);
		map.put("belongType", belongType);
		map.put("feeTypePID", feeTypePID);

		List<PropFeeTypeDto> list = this.getSqlSession().selectList(
				"PropFeeTypeSearchMapper.findPropFeeTypeDtoByRelevantID", map);
		return list;
	}

	@Override
	public Map<String, PropFeeTypeDto> getFeeTypePrice(String commID,
			String apartmentID) throws AppBizException {
		Map<String, String> param = new HashMap<String, String>();
		param.put("commID", commID);
		param.put("apartmentID", apartmentID);
		List<Map<String, Object>> rules = this.getSqlSession().selectList(
				"computeRuleMapper.getDsRules2", param);
		Map<String, PropFeeTypeDto> m = new HashMap<>();
		for (Map<String, Object> rule : rules) {
			PropFeeTypeDto p = new PropFeeTypeDto();
			m.put(rule.get("fee_type_id").toString(), p);
			if ("0".equals(rule.get("type").toString())) {
				p.setPrice(rule.get("rule").toString().split(";")[0]);
			} else if ("1".equals(rule.get("type").toString())) {
				if (rule.get("rule").toString().split(";").length > 2) {

					p.setPrice(rule.get("rule").toString().split(";")[2]);
				}
			} else {

				Map<String, Object> j = JSON.parseObject(
						StringUtils.toString((rule.get("param_val"))),
						Map.class);

				TypeReference<List<Map<String, String>>> t = new TypeReference<List<Map<String, String>>>() {
				};
				List<Map<String, String>> rs = JSON.parseObject(j.get("ratios")
						.toString(), t);
				StringBuffer b = new StringBuffer();
				for (Map<String, String> map2 : rs) {
					b.append(map2.get("start")).append("-")
							.append(map2.get("end")).append(":")
							.append(map2.get("ratio")).append("元;");
				}

				p.setPrice(b.toString());

			}
			p.setFeeAmountType("0");
			if (!rule.get("type").equals("0")) {

				if (rule.get("feeType").equals("0")) {
					p.setFeeAmountType("1");

				} else {

					p.setFeeAmountType("2");
				}

			}

		}

		return m;
	}

}